﻿using System.Xml;
namespace ManagerClinic.CanLamSang
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Linq;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using System.Configuration;
    using ManagerClinic.Ultilites.HtmlEditor.Buttons;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmPhieuPhauThuatThuThuatTaiCho : DevExpress.XtraEditors.XtraForm
    {
        string _MaPhieuPhauThuatThuThuatTaiCho = string.Empty, _MaHoSoKhamBenh = string.Empty, _MaDichVu = string.Empty;
        Int32 _status;
        bool resultInsert = false;
        public event EventHandler _SavePhieuCanLamSang = null;

        PhieuPhauThuatThuThuatTaiChoEntity phieuPhauThuatThuThuatTaiChoEntity = null;
        HoSoKhamBenhNgoaiTruEntity hoSoKhamBenhNgoaiTruEntity = null;

        HoSoKhamBenhNgoaiTruBusiness hoSoKhamBenhNgoaiTruBusiness = new HoSoKhamBenhNgoaiTruBusiness();
        ThuPhiDichVuBusiness thuPhiDichVuBusiness = new ThuPhiDichVuBusiness();
        DmBacSyBusiness dmBacSyBusiness = new DmBacSyBusiness();
        PhieuPhauThuatThuThuatTaiChoBusiness phieuPhauThuatThuThuatTaiChoBusiness = new PhieuPhauThuatThuThuatTaiChoBusiness();
        PhieuPhauThuatThuThuatTaiChoChiTietBusiness phieuPhauThuatThuThuatTaiChoChiTietBusiness = new PhieuPhauThuatThuThuatTaiChoChiTietBusiness();

        public frmPhieuPhauThuatThuThuatTaiCho(Int32 status, string maDichVu, HoSoKhamBenhNgoaiTruEntity obj)
        {
            InitializeComponent();

            PredefinedButtonSets.SetupDefaultButtons(this.txtKetQua);
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);

            _status = status;
            resultInsert = false;
            hoSoKhamBenhNgoaiTruEntity = obj;
            _MaDichVu = maDichVu;

            _MaPhieuPhauThuatThuThuatTaiCho = ConstantManagerClinic.setCodeManagerClinic(ConstantManagerClinic.maPhieuPhauThuatTTTaiCho);

            ucThongTinHanhChinh.LoadThongTinHanhChinh(hoSoKhamBenhNgoaiTruEntity.MaBenhNhan);
            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(_MaDichVu);

        }
        public frmPhieuPhauThuatThuThuatTaiCho(Int32 status, string maPhieuPhauThuatTTTC)
        {
            InitializeComponent();

            PredefinedButtonSets.SetupDefaultButtons(this.txtKetQua);
            PredefinedButtonSets.SetupDefaultButtons(this.txtKetLuan);

            _status = status;
            resultInsert = false;

            _MaPhieuPhauThuatThuThuatTaiCho = maPhieuPhauThuatTTTC;
            phieuPhauThuatThuThuatTaiChoEntity = phieuPhauThuatThuThuatTaiChoBusiness.GetPhieuPhauThuatThuThuatTaiCho(_MaPhieuPhauThuatThuThuatTaiCho);
            hoSoKhamBenhNgoaiTruEntity = hoSoKhamBenhNgoaiTruBusiness.GetHoSoKhamBenhNgoaiTru(phieuPhauThuatThuThuatTaiChoEntity.MaHoSo);
            _MaDichVu = phieuPhauThuatThuThuatTaiChoEntity.MaDichVu;

            ucThongTinHanhChinh.LoadThongTinHanhChinh(hoSoKhamBenhNgoaiTruEntity.MaBenhNhan);
            ucChiDinhKhoaPhong.LoadDmKhoaKham();
            ucChiDinhKhoaPhong.LoadDmKhoaCanLamSang();
            ucChiDinhKhoaPhong.LoadDmPhongKham();
            ucChiDinhKhoaPhong.LoadDmPhongCanLamSang();
            ucChiDinhKhoaPhong.setMaKhoaKham(phieuPhauThuatThuThuatTaiChoEntity.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(phieuPhauThuatThuThuatTaiChoEntity.MaPhongKham);
            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(phieuPhauThuatThuThuatTaiChoEntity.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(phieuPhauThuatThuThuatTaiChoEntity.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.LoadDmDichVuChiTiet(_MaDichVu);

            LoadInfoPhieu(phieuPhauThuatThuThuatTaiChoEntity);
        }
        void LoadInfoPhieu(PhieuPhauThuatThuThuatTaiChoEntity obj)
        {
            ucChiDinhKhoaPhong.setMaKhoaKham(obj.MaKhoaKham);
            ucChiDinhKhoaPhong.setMaPhongKhamBenh(obj.MaPhongKham);
            ucChiDinhKhoaPhong.setMaKhoaCanLamSang(obj.MaKhoaCanLamSang);
            ucChiDinhKhoaPhong.setMaPhongCanLamSang(obj.MaPhongCanLamSang);
            ucChiDinhKhoaPhong.setBacSy(obj.MaBacSyKham);

            txtKetLuan.Html = obj.KetLuan;
            txtKetQua.Html = obj.KetQua;

            var dmBacSyClsEntity = dmBacSyBusiness.GetDmBacSy(obj.MaBacSyCls);
            if (dmBacSyClsEntity != null)
                txtBacSyCls.Text = dmBacSyClsEntity.MaBacSy + " - " + dmBacSyClsEntity.TenBacSy;
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.IsValidLookupEdit();
            ucChiDinhKhoaPhong.IsValidChooseDichVuChiTiet();
            if (resultInsert == false)
            {
                phieuPhauThuatThuThuatTaiChoEntity = new PhieuPhauThuatThuThuatTaiChoEntity()
                {
                    MaPhauThuatThuThuat = _MaPhieuPhauThuatThuThuatTaiCho.Trim(),
                    Stt = 0,
                    SoVaoVien = hoSoKhamBenhNgoaiTruEntity.SoVaoVien,
                    MaHoSo = hoSoKhamBenhNgoaiTruEntity.MaHoSo,
                    NgayVaoKham = hoSoKhamBenhNgoaiTruEntity.NgayVaoKham,
                    MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham(),
                    MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham(),
                    MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang(),
                    MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang(),
                    MaDichVu = _MaDichVu,
                    KetLuan = txtKetLuan.Html.Trim(),
                    KetQua = txtKetQua.Html.Trim(),
                    YeuCau = ucChiDinhKhoaPhong.getYeuCau(),
                    GhiChu = ucChiDinhKhoaPhong.getGhiChu(),
                    TrangThaiPhieu = (int)ConstantManagerClinic.TrangThaiPhieuCanLamSang.TaoMoi,
                    MaBacSyCls = string.Empty,
                    MaBacSyKham = Authenticate.MaBacSy,
                    TrangThaiCho = Convert.ToBoolean(ConstantManagerClinic.TrangThaiChoDichVuKhamChuaBenh.ChuaThucHien)
                };
                resultInsert = phieuPhauThuatThuThuatTaiChoBusiness.InsertPhieuPhauThuatThuThuatTaiCho(phieuPhauThuatThuThuatTaiChoEntity);
            }
            else
            {
                phieuPhauThuatThuThuatTaiChoEntity = phieuPhauThuatThuThuatTaiChoBusiness.GetPhieuPhauThuatThuThuatTaiCho(_MaPhieuPhauThuatThuThuatTaiCho);
                phieuPhauThuatThuThuatTaiChoEntity.YeuCau = ucChiDinhKhoaPhong.getYeuCau();
                phieuPhauThuatThuThuatTaiChoEntity.GhiChu = ucChiDinhKhoaPhong.getGhiChu();
                phieuPhauThuatThuThuatTaiChoEntity.KetQua = txtKetQua.Html.Trim();
                phieuPhauThuatThuThuatTaiChoEntity.KetLuan = txtKetLuan.Html.Trim();
                phieuPhauThuatThuThuatTaiChoEntity.MaKhoaCanLamSang = ucChiDinhKhoaPhong.getMaKhoaCanLamSang();
                phieuPhauThuatThuThuatTaiChoEntity.MaPhongCanLamSang = ucChiDinhKhoaPhong.getMaPhongCanLamSang();
                phieuPhauThuatThuThuatTaiChoEntity.MaDichVu = _MaDichVu;
                phieuPhauThuatThuThuatTaiChoEntity.MaKhoaKham = ucChiDinhKhoaPhong.getMaKhoaKham();
                phieuPhauThuatThuThuatTaiChoEntity.MaPhongKham = ucChiDinhKhoaPhong.getMaPhongKham();

                phieuPhauThuatThuThuatTaiChoBusiness.EditPhieuPhauThuatThuThuatTaiCho(phieuPhauThuatThuThuatTaiChoEntity);
            }

            //delete from table ThuPhiDichVu
            thuPhiDichVuBusiness.DeleteThuPhiDichVuByMaPhieuDichVu(_MaPhieuPhauThuatThuThuatTaiCho);
            //delete from table PhieuPhauThuatThuThuatTaiChoChiTiet
            phieuPhauThuatThuThuatTaiChoChiTietBusiness.DeletePhieuPhauThuatThuThuatTaiChoChiTiet_ByMaPhauThuatThuThuat(_MaPhieuPhauThuatThuThuatTaiCho);

            if (resultInsert == true)
            {
                //Insert into table ThuPhiDichVu, PhieuSieuAmChiTiet
                IList<DmDichVuChiTietEntity> lstDmDichVuChiTietIsChecked = ucChiDinhKhoaPhong.DmDichVuChiTietEntityIsChecked;
                if (lstDmDichVuChiTietIsChecked == null) { return; }

                foreach (DmDichVuChiTietEntity dmDichVuChiTietEntity in lstDmDichVuChiTietIsChecked)
                {
                    var thuphiDichVuEntity = new ThuPhiDichVuEntity()
                    {
                        MaPhieuDichVu = _MaPhieuPhauThuatThuThuatTaiCho,
                        MaHoSo = phieuPhauThuatThuThuatTaiChoEntity.MaHoSo,
                        NgayDenKham = phieuPhauThuatThuThuatTaiChoEntity.NgayVaoKham,
                        MaDichVu = _MaDichVu,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        TongTien = dmDichVuChiTietEntity.DonGia,
                        DaDongTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien,
                        MaNguoiDung = Authenticate.UserName,
                        MaKhoaKham = phieuPhauThuatThuThuatTaiChoEntity.MaKhoaKham,
                        MaPhongKham = phieuPhauThuatThuThuatTaiChoEntity.MaPhongKham,
                        MaKhoaCanLamSang = phieuPhauThuatThuThuatTaiChoEntity.MaKhoaCanLamSang,
                        MaPhongCanLamSang = phieuPhauThuatThuThuatTaiChoEntity.MaPhongCanLamSang,
                        DaThucHien = Convert.ToBoolean(ConstantManagerClinic.TrangThaiThucHienDichVuKhamChuaBenh.ChuaThucHien)
                    };
                    thuPhiDichVuBusiness.InsertThuPhiDichVu(thuphiDichVuEntity);

                    var phieuPhauThuatThuThuatTaiChoChiTietEntity = new PhieuPhauThuatThuThuatTaiChoChiTietEntity()
                    {
                        MaPhauThuatThuThuat = _MaPhieuPhauThuatThuThuatTaiCho,
                        MaChiTieu = dmDichVuChiTietEntity.MaChiTieu,
                        DonGia = dmDichVuChiTietEntity.DonGia,
                        DaThuTien = (int)ConstantManagerClinic.TrangThaiDongTien.ChuaDongTien
                    };
                    phieuPhauThuatThuThuatTaiChoChiTietBusiness.InsertPhieuPhauThuatThuThuatTaiChoChiTiet(phieuPhauThuatThuThuatTaiChoChiTietEntity);
                }

                MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            if (_SavePhieuCanLamSang != null) _SavePhieuCanLamSang(sender, e);
        }

        private void btnLuuKetQua_Click(object sender, EventArgs e)
        {
            phieuPhauThuatThuThuatTaiChoEntity = phieuPhauThuatThuThuatTaiChoBusiness.GetPhieuPhauThuatThuThuatTaiCho(_MaPhieuPhauThuatThuThuatTaiCho);
            phieuPhauThuatThuThuatTaiChoEntity.KetQua = txtKetQua.Html;
            phieuPhauThuatThuThuatTaiChoEntity.KetLuan = txtKetLuan.Html;
            phieuPhauThuatThuThuatTaiChoEntity.MaBacSyCls = Authenticate.MaBacSy;
            phieuPhauThuatThuThuatTaiChoBusiness.EditPhieuPhauThuatThuThuatTaiCho(phieuPhauThuatThuThuatTaiChoEntity);
            MessageBox.Show("Cập nhật kết quả thành công!", ConstantManagerClinic.sTitleInformation, MessageBoxButtons.OK);
        }

        private void btnDongKetQua_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmPhieuPhauThuatThuThuatTaiCho_Load(object sender, EventArgs e)
        {
            ucChiDinhKhoaPhong.setDmDichVuChiTietEntityIsChecked(_MaPhieuPhauThuatThuThuatTaiCho, _MaDichVu);
        }
    }
}